/*
Copyright (c) 2008 SmashedApples 

Contributors.  See: http://smashedapples.googlecode.com/

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package smashedapples.containers.form
{
	import flash.utils.Dictionary;
	
	import mx.core.Container;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import smashedapples.controls.form.IFormControl;
	import smashedapples.core.ValueObject;
	import smashedapples.events.DataEntryFormEvent;
	
	[Event(name="submit", type="smashedapples.events.DataEntryFormEvent")]
	
	public class DataEntryForm extends BaseForm
	{

		public var controls : Dictionary = new Dictionary();
		
			
		public function DataEntryForm():void
		{
			super();
		}
		
		private var _dataProvider : Object;
		public function get dataProvider() : Object
		{
			if( _dataProvider == null)
				_dataProvider = new itemClass(); 
			
			return _dataProvider;
		} 
		public function set dataProvider(value:*) : void 
		{
			
			_dataProvider = value;
			this.setControls( value );
		} 
		
		
		
		
		public var disableOnSubmit : Boolean = false;
		
		
		
		
		public function get isInsert():Boolean
		{	
			if(this.dataProvider.itemID == null)
				return true;
				
			if(this.dataProvider.itemID.length == 0)
				return true;
		
			return false;
		}
		public function get isUpdate():Boolean
		{
			return (isInsert) ? false : true;
		}
		
		
		

		private var initialArgs : Object = {}; 
		
		public function setControls(args:Object=null):void
		{	
			
			if(args == null)
				return void;
			
			this.initialArgs = args;

			//trace(mx.utils.ObjectUtil.toString(args));
			if(args != null)
			{	
				var controls : Array = getControls();
				for each(var control : IFormControl in controls)
				{	
					
					if( args.hasOwnProperty( control.key ) )
					{
						control.val = args[ control.key ];
						
					}
				}
			}

		}
	
		
		public var itemClass:Class;
		
		public function clear():void
		{	
			this.dataProvider = {};
			var obj : * = new itemClass();
			this.setControls( obj ); 
		}
		
		public function reset():void
		{	
			this.setControls( this.initialArgs ); 
		}
		
		
		public function getControls():Array
		{	
			_controls = new Array();
			getControlsImpl(this.getChildren());
			return _controls;
		}
		
		[ArrayElementType("smashedapples.controls.form.IFormControl")]
		private var _controls : Array;
		
		private function getControlsImpl(children:Array):void
		{	
		
			for( var child:String in children ) 
			{
				if( children[child] is IFormControl && children[child] != null)
				{	
					var control:IFormControl = children[ child ];
						_controls.push( control);

				}
				else if( children[ child ] is Container)
				{
					getControlsImpl( (children[ child ] as Container).getChildren() );
				}	
			}
		}
	
		public function submit(...args):void
		{	
			dispatchEvent( new DataEntryFormEvent("submit",{form:this} ));
		}
		
	
		public function getValues():Object
		{	
			this._args = this.dataProvider;
			this.controls = new Dictionary();
			getArgsImpl(this.getChildren());
			
			return _args;
		}
		
		private var _args : Object;
		
		private function getArgsImpl(children:Array):void
		{
			for( var child:String in children ) 
			{
				if( children[child] is IFormControl && children[child] != null)
				{	
					
					var control:IFormControl = children[ child ];
					this.controls[control.key] = control;
					
					if(this.isInsert)
					{
						if(control.val != null && control.val != "")
							this._args[control.key] = control.val;		
					}
					else
					{
						if(control.val != this.initialArgs[control.key])
							this._args[control.key] = control.val;	
					}
					
					if( control.hasChanged() )
					{	
						this._args[control.key] = control.val;	
					}
					
				}
				else if( children[ child ] is Container)
				{
					getArgsImpl( (children[ child ] as Container).getChildren() );
				}	

			}
		}
		
		
		
		
		
		
		
		
		
	}
}